﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace RestaurantWebService
{
    public class KitchenManager
    {
        private const string placeOrderSql = "INSERT INTO order(tableNumber, seatNumber, mealNumber, status) VALUES({0}, {1}, {2}, 'Placed')";
        private const string ordersToBeDeliveredSql = "SELECT * FROM order WHERE status = 'Ready'";
        private const string pendingOrdersSql = "SELECT * FROM order WHERE status = 'Placed'";
        private const string acknowledgeDeliverySql = "UPDATE order SET status = 'Delivered' WHERE orderNumber = {0}";
        private const string indicateReadySql = "UPDATE order SET status = 'Ready' WHERE orderNumber = {0}";
		
		private DatabaseManager dbManager;

        public KitchenManager()
        {
            dbManager = new DatabaseManager();
        }

        public int placeOrder(Order order)
        {
            dbManager.executeNonQuery(String.Format(placeOrderSql, order.TableNumber, order.SeatNumber, order.Meal.MealNumber));
            return (int) dbManager.executeScalar("SELECT SCOPE_IDENTITY()");
        }

        public List<Order> getOrdersToBeDelivered()
        {
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                conn = dbManager.getConnection();
                reader = dbManager.getDataReader(ordersToBeDeliveredSql, conn);
                return createOrderList(reader);
            }
            finally
            {
                DatabaseManager.close(reader);
                DatabaseManager.close(conn);
            }
        }

        public void acknowledgeDelivery(Order order)
        {
            dbManager.executeNonQuery(String.Format(acknowledgeDeliverySql, order.OrderNumber));
        }

         public List<Order> getPendingOrders()
        {
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                conn = dbManager.getConnection();
                reader = dbManager.getDataReader(pendingOrdersSql, conn);
                return createOrderList(reader);
            }
            finally
            {
                DatabaseManager.close(reader);
                DatabaseManager.close(conn);
            }
        }

        public void indicateReady(Order order)
        {
            dbManager.executeNonQuery(String.Format(indicateReadySql, order.OrderNumber));
        }

        private List<Order> createOrderList(SqlDataReader reader)
        {
            List<Order> orders = new List<Order>();
            while (reader.Read())
            {
                Order order = new Order();
                order.OrderNumber = (int) reader["orderNumber"];
                order.TableNumber = (int) reader["tableNumber"];
                order.SeatNumber = (int) reader["seatNumber"];
                order.Meal = Menu.Instance.getMealByNumber((int) reader["mealNumber"]);
                order.OrderTime = (DateTime) reader["orderTime"];
                order.Status = (OrderStatus) Enum.Parse(typeof(OrderStatus), (string) reader["status"], false);
                orders.Add(order);
            }
            return orders;
        }
    }
}